package com.jichangxiu.framework.config;

import com.jichangxiu.common.constant.Constants;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.GroupedOpenApi;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Slf4j
@Configuration
public class SwaggerConfig {

    @Bean
    public GroupedOpenApi userApi() {
        log.debug("【SwaggerConfig】加载【GroupedOpenApi】配置");
        String[] paths = {"/**"};
        String[] packagedToMatch = {Constants.JCX_PROPERTIES.getSwaggerProperties().getBasePackage()};
        return GroupedOpenApi.builder().group(Constants.JCX_PROPERTIES.getSwaggerProperties().getTitleAndGroup())
                .pathsToMatch(paths)
                .packagesToScan(packagedToMatch).build();
    }

    @Bean
    public OpenAPI customOpenAPI() {
        log.debug("【SwaggerConfig】加载【OpenAPI】配置");
        Contact contact = new Contact();
        contact.setName(Constants.JCX_PROPERTIES.getSwaggerProperties().getContactName());
        contact.setEmail(Constants.JCX_PROPERTIES.getSwaggerProperties().getContactEmail());
        contact.setUrl(Constants.JCX_PROPERTIES.getSwaggerProperties().getContactUrl());
        return new OpenAPI().info(new Info()
                .title(Constants.JCX_PROPERTIES.getSwaggerProperties().getTitleAndGroup())
                .description(Constants.JCX_PROPERTIES.getSwaggerProperties().getDescription())
                .contact(contact)
                .version(Constants.JCX_PROPERTIES.getSwaggerProperties().getVersion())
                .termsOfService(Constants.JCX_PROPERTIES.getSwaggerProperties().getTermsOfServiceUrl())
                .license(new License().name(Constants.JCX_PROPERTIES.getSwaggerProperties().getLicense())
                        .url(Constants.JCX_PROPERTIES.getSwaggerProperties().getLicenseUrl())));
    }

}
